<template>
  <div class="case-print-view" v-if="caseData">
    <caseHeader :case-data="caseData"></caseHeader>
    <div
      style="min-height: 200px"
    >
      <div class="prescription-container" v-if="checkItemList.length||surchargeList.length">
        <check-item-list
          :checkList="checkItemList"
          :surchargeList="surchargeList"
        ></check-item-list>
      </div>
      <template v-for="(item, index) in chengyaoList">
        <div
          class="prescription-container"
          v-if="chengyaoList.length"
          :key="'cycf' + index"
        >
          <div class="item-title">
            <span>成药处方{{ index + 1 }}：</span>
          </div>
          <cy-prescription-list :prescriptionItem="item"></cy-prescription-list>
        </div>
      </template>
      <template v-for="(item, index) in yinpianList">
        <div
          class="prescription-container"
          v-if="yinpianList.length"
          :key="'ypcf' + index"
        >
          <div class="item-title">
            <span>饮片处方{{ index + 1 }}：</span>
          </div>
          <yp-prescription-list :prescriptionItem="item"></yp-prescription-list>
        </div>
      </template>
      <template v-for="(item, index) in shuyeList">
        <div
          class="prescription-container"
          v-if="shuyeList.length"
          :key="'sycf' + index"
        >
          <div class="item-title">
            <span>输液处方{{ index + 1 }}：</span>
          </div>
          <sy-prescription-list :prescriptionItem="item"></sy-prescription-list>
        </div>
      </template>
    </div>
    <case-footer :caseFooterData="caseData"></case-footer>
  </div>
</template>
<script>
import caseHeader from "./caseHeader";
import caseFooter from "./caseFooter";
import checkItemList from "./checkItemList.vue";
import cyPrescriptionList from "./cyPrescriptionList.vue";
import ypPrescriptionList from "./ypPrescriptionList.vue";
import syPrescriptionList from "./syPrescriptionList.vue";
export default {
  props: {
    caseData: {},
  },
  components: {
    caseHeader,
    caseFooter,
    checkItemList,
    cyPrescriptionList,
    ypPrescriptionList,
    syPrescriptionList,
  },
  data: function () {
    return {
      checkItemList: [],
      checkItemFooter: {},
      chengyaoList: [],
      yinpianList: [],
      shuyeList: [],
      surchargeList: [],
      dataLength: 0,
    };
  },
  watch: {
    caseData: {
      handler(newval, oldval) {
        if (newval) {
          if (!Array.isArray(newval.age)) {
            newval.age = newval.age.split(",").map((value) => {
              return Number(value);
            });
          }
          if (newval.check_items) {
            this.checkItemFooter = {
              doctor_name: newval.doctor_name,
              amount: 0,
            };
            this.checkItemList = [];
            newval.check_items.forEach((check, checkidx) => {
              let priceAmount =
                Number(check.check_item_number) * Number(check.price);
              check.amount = priceAmount;
              this.checkItemFooter.amount += priceAmount;
              this.checkItemList.push(check);
            });
          }
          this.surchargeList = [];
          if (newval.surcharge) {
            let total = 0;
            newval.surcharge.forEach((itm, idx) => {
              total += itm.amount;
              this.surchargeList.push(itm)
            });
            this.checkItemFooter.amount += total;
          }
          this.chengyaoList = [];
          this.yinpianList = [];
          this.shuyeList = [];
          if (newval && newval.prescriptions && newval.prescriptions.length > 0) {
            
            newval.prescriptions.forEach((item, index) => {
              let footerData = {
                doctor_name: newval.doctor_name,
                amount: 0,
              };
              Object.assign(item, { amount: 0, footerData: footerData });
              if (item.details) {
                item.details.forEach((det, detidx) => {
                  item.amount += Number(det.price) * Number(det.number);
                });
              }
              item.footerData.amount = item.amount;

              if (item.category_key == "cycf") {
                this.chengyaoList.push(item);
              } else if (item.category_key == "ypcf") {
                this.yinpianList.push(item);
              } else {
                this.shuyeList.push(item);
              }
            });
          }else{
            this.chengyaoList = [];
            this.yinpianList = [];
            this.shuyeList = [];
          }
          this.dataLength =
            this.checkItemList.length +
            this.surchargeList.length +
            this.chengyaoList.length +
            this.yinpianList.length +
            this.shuyeList.length;
          console.log(this.dataLength);
        }
      },
      immediate: true,
      deep: true
    },
  },
  mounted: function () {},
  methods: {},
};
</script>
<style lang="scss" scoped>
@import "@/assets/scss/print/casePrint.scss";
</style>
